#include <string.h>
#include <stdlib.h>

#include "lib.h"
#include "libglob.h"
#include "bitmap.h"
#include "compimg.h"
#include "rmanager.h"
#include "rresource.h"


pCBitmap
Library::newCompImage(const String & name)
{
	pCompImageResourceHeader	header;
	pCompImageResourceEntry		entry;
	pCBitmap					bitmap = 0;
	pCBitmap					piece;
	int32						x;
	int32						y;
	uint32						i;

	// Load the resource.
	header = (pCompImageResourceHeader)gResourceManager->get(name);
	if (header)
	{
		if (header->m_version_major == 1 || header->m_version_minor == 0)
		{
			// Calculate the pointer to the first entry.
			entry = (pCompImageResourceEntry)
						((uint8 *)header + sizeof CompImageResourceHeader);

			// Iterate through all the pieces.
			for (i = 0; i < header->m_entries; i++)
			{
				if (stricmp(entry->m_name, "felt") == 0)
				{
					if (!bitmap)
					{
						bitmap = newCBitmapBlank(entry->m_x, entry->m_y);
						if (!bitmap)
							break;

						CBitmapFillRect(bitmap, 0, 0, CBitmapWidth(bitmap), CBitmapHeight(bitmap), (COLORSIZE)gFeltColor);
					}
					else
					{
						CBitmapFillRect(bitmap, 0, 0, CBitmapWidth(bitmap), CBitmapHeight(bitmap), (COLORSIZE)gFeltColor);
					}
				}
				else if (stricmp(entry->m_name, "shade") == 0)
				{
					if (!bitmap)
					{
						bitmap = newCBitmapBlank(entry->m_x, entry->m_y);
						if (!bitmap)
							break;

						CBitmapShade(bitmap, (COLORSIZE)gFeltColor);
					}
					else
					{
						CBitmapShade(bitmap, (COLORSIZE)gFeltColor);
					}
				}
				else
				{
					piece = newCBitmap(entry->m_name, 0);
					if (piece)
					{
						if (!bitmap)
						{
							bitmap = newCBitmapBlank(CBitmapWidth(piece), CBitmapHeight(piece));
							if (!bitmap)
								break;
						}

						switch (entry->m_alignment & PRIVATE_CIA_X)
						{
							case PRIVATE_CIA_LEFT:
								x = entry->m_x;
								break;

							case PRIVATE_CIA_CENTER:
								x = entry->m_x - (CBitmapWidth(piece) >> 1);
								break;

							case PRIVATE_CIA_RIGHT:
								x = entry->m_x - CBitmapWidth(piece);
								break;

							default:
								x = entry->m_x;
								break;
						}

						switch (entry->m_alignment & PRIVATE_CIA_Y)
						{
							case PRIVATE_CIA_TOP:
								y = entry->m_y;
								break;

							case PRIVATE_CIA_MIDDLE:
								y = entry->m_y - (CBitmapHeight(piece) >> 1);
								break;

							case PRIVATE_CIA_BOTTOM:
								y = entry->m_y - CBitmapHeight(piece);
								break;

							default:
								y = entry->m_y;
						}

		#pragma revisit("Restore this to CBitmapBitmapBlt once all the resources have been updated.")
						CBitmapTransparentBitmapBlt(bitmap, x, y, piece);

						deleteCBitmap(piece);
					}
				}

				// Advance to the next entry.
				entry = (pCompImageResourceEntry)((uint8 *)entry + entry->m_size);
			}
		}

		Resource::release(header);
	}

	return bitmap;
}


String
Library::newCompImageDetails(const String & name)
{
	String						info;
	String						temp;
	pCompImageResourceHeader	header;
	pCompImageResourceEntry		entry;
	uint32						i;

	// Load the resource.
	header = (pCompImageResourceHeader)gResourceManager->get(name);
	if (header)
	{
		info.sprintf("\n\n"
					 "header.version_major = %d\n"
					 "header.version_minor = %d\n"
					 "header.entries       = %d\n\n",
					 header->m_version_major,
					 header->m_version_minor,
					 header->m_entries);

		if (header->m_version_major == 1 || header->m_version_minor == 0)
		{
			// Calculate the pointer to the first entry.
			entry = (pCompImageResourceEntry)
						((uint8 *)header + sizeof CompImageResourceHeader);

			// Iterate through all the pieces.
			for (i = 0; i < header->m_entries; i++)
			{
				temp.sprintf("%02d: %s,%d,%d", i, entry->m_name, entry->m_x, entry->m_y);

				if (stricmp(entry->m_name, "felt") == 0)
					temp += "\n";
				else
				{
					switch (entry->m_alignment)
					{
						case CIA_TL:	temp += ",TL\n";		break;
						case CIA_TC:	temp += ",TC\n";		break;
						case CIA_TR:	temp += ",TR\n";		break;
						case CIA_ML:	temp += ",ML\n";		break;
						case CIA_MC:	temp += ",MC\n";		break;
						case CIA_MR:	temp += ",MR\n";		break;
						case CIA_BL:	temp += ",BL\n";		break;
						case CIA_BC:	temp += ",BC\n";		break;
						case CIA_BR:	temp += ",BR\n";		break;
						default:		temp += ",Unknown\n";	break;
					}
				}

				info += temp;

				// Advance to the next entry.
				entry = (pCompImageResourceEntry)((uint8 *)entry + entry->m_size);
			}
		}
		else
			info += "Unknown version number.  Upgrade your ResourceViewer.\n";

		Resource::release(header);
	}

	return info;
}


//
// vim: nowrap
//
